package com.dianhun.es.utils;

import com.dianhun.es.pojo.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

/**
 * @author cyt
 * @create 2020-08-26 20:21
 */
public class GetHtmlDate {

    public ArrayList<Content> getDate(String keyword) throws IOException {

        ArrayList<Content> list = new ArrayList<>();

        String url = "https://search.jd.com/Search?keyword=" + keyword;
        Document parse = Jsoup.parse(new URL(url), 300000);
        Element j_goodsList = parse.getElementById("J_goodsList");
        Elements li = j_goodsList.getElementsByTag("li");
        for (Element element : li) {
            // source-data-lazy-imgs
            String img = element.getElementsByTag("img").eq(0).attr("src");
            String price = element.getElementsByClass("p-price").eq(0).text();
            String title = element.getElementsByClass("p-name").eq(0).text();

            System.out.println("============================");
            System.out.println(img + " - > " + price + " - > " + title);

            Content content = new Content(title, img, price);
            list.add(content);
        }
        return list;
    }

}
